package com.daniel.lambdatest;

import org.junit.Test;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;

/**
 * Created by daniel on 2015/7/15.
 */
public class LambdaTest {
    @Test
    public void inThread() {
        //Before Java 8:
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Before Java8 ");
            }
        }).start();
        //Java 8 way:
        new Thread(() -> System.out.println("In Java8!")).start();
    }

    @Test
    public void demo1() {
        //Get count, min, max, sum, and average for numbers
        List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
        IntSummaryStatistics stats = primes.stream().mapToInt((x) -> x)
                .summaryStatistics();
        System.out.println("Highest prime number in List : " + stats.getMax());
        System.out.println("Lowest prime number in List : " + stats.getMin());
        System.out.println("Sum of all prime numbers : " + stats.getSum());
        System.out.println("Average of all prime numbers : " + stats.getAverage());
    }


    @Test
    public void foreach() {
        //Prior Java 8 :
        List<String> features = Arrays.asList("Lambdas", "Default Method",
                "Stream API", "Date and Time API");
        for (String feature : features) {
            System.out.println(feature);
        }
        ////In Java 8:
        //        List<String> feature1s = Arrays.asList("Lambdas", "Default Method", "Stream API",
        //                "Date and Time API");
        features.forEach(feature -> System.out.println(feature));
    }
}
